package com.example.robertluozizhao.framecollectdemo.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by robert.luozizhao on 2018/1/10.
 */

public class SwitchButtonView extends View {

    private Bitmap btnSwitchOff;
    private Bitmap btnSwitchOn;
    private Bitmap ball;


    private ToggleState toggleState;
    private float distance;
    private boolean isOpen;
    private float ballToRight;
    private boolean isScroll;
    private int ballWidth;


    public SwitchButtonView(Context context) {
        this(context, null);
    }

    public SwitchButtonView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    // 控制view大小
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int offWidth = btnSwitchOn.getWidth();
        int height = btnSwitchOn.getHeight();
        setMeasuredDimension(offWidth, height);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (ball==null|| btnSwitchOn ==null|| btnSwitchOff ==null) {
            return;
        }
        ballWidth = ball.getWidth();
        ballToRight = btnSwitchOn.getWidth() - ball.getWidth();
        drawBtn(canvas,isScroll);
/*
        //边界限制
        if (ball.getWidth() > distance) {
            canvas.drawBitmap(ball, 0, 0, null);
        }
        if (ball.getWidth() > ballToRight) {
            canvas.drawBitmap(ball, ballToRight, 0, null);
        }
*/
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    private void drawBtn(Canvas canvas, boolean isScroll) {
        if (isScroll) {
            drawScroll(canvas);
        }else {
            drawNoScroll(canvas);
        }
    }

    private void drawNoScroll(Canvas canvas) {
        if (distance<btnSwitchOff.getWidth()/2){
            canvas.drawBitmap(btnSwitchOff,0,0,null);
            canvas.drawBitmap(ball,0,0,null);
        }else {
            canvas.drawBitmap(btnSwitchOn,0,0,null);
            canvas.drawBitmap(ball,ballToRight,0,null);
        }
    }

    private void drawScroll(Canvas canvas) {
        if (distance<btnSwitchOff.getWidth()/2){
            canvas.drawBitmap(btnSwitchOn,0,0,null);
            if (distance<ballWidth) {
                canvas.drawBitmap(ball,0,0,null);
            }else if(distance>=ballWidth&&distance<ballToRight){
                canvas.drawBitmap(ball,distance-(ballWidth/2),0,null);
            }
        }else if(distance>=btnSwitchOff.getWidth()/2){
            canvas.drawBitmap(btnSwitchOff,0,0,null);
            if (distance>ballToRight) {
                canvas.drawBitmap(ball,ballToRight,0,null);
            }else if(distance>=ballWidth&&distance<ballToRight){
                canvas.drawBitmap(ball,distance-(ballWidth/2),0,null);
            }
        }
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX(); // 触发位置距自己（容器）
        float rawX = event.getRawX();//触发位置距屏幕边
        Log.d("diy", "onTouchEvent: x:" + x + ",rawX:" + rawX);
        distance = x + 0.5f;

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isScroll = true;
                break;
            case MotionEvent.ACTION_MOVE:
                isScroll = true;
                break;
            case MotionEvent.ACTION_UP:
                isScroll = false;
                setIsOpen();
                break;
        }
        //重新执行 ondraw();重绘
        invalidate();
        //true 子view已消费事件
        return true;
    }

    private void setIsOpen() {
        if (distance< btnSwitchOn.getWidth()/2) {
            if (toggleState!=null) {
                this.isOpen = false;
                toggleState.ShowToggleState(this,isOpen);
            }
        }else if(distance> btnSwitchOn.getWidth()/2){
            if (toggleState!=null) {
                this.isOpen = true;
                toggleState.ShowToggleState(this,isOpen);
            }
        }
    }


    public Bitmap getBtnSwitchOff() {
        return btnSwitchOff;
    }

    public void setBtnSwitchOff(int ivRes) {
        this.btnSwitchOff = BitmapFactory.decodeResource(getResources(), ivRes);
    }

    public Bitmap getBtnSwitchOn() {
        return btnSwitchOn;
    }

    public void setBtnSwitchOn(int ivRes) {
        this.btnSwitchOn = BitmapFactory.decodeResource(getResources(), ivRes);
    }

    public Bitmap getBall() {
        return ball;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void setBall(int ivRes) {
        this.ball = BitmapFactory.decodeResource(getResources(), ivRes);
    }

    public void setToggleState(boolean isOpen) {
        this.isOpen = isOpen;
    }

    public boolean getToggleState() {
        return this.isOpen;
    }

    public void setToggleState(ToggleState toggleState) {
        this.toggleState = toggleState;
    }


    public interface ToggleState {
        void ShowToggleState(SwitchButtonView switchButtonView, boolean isOpen);
    }

}
